//
//  NoticeListCell.swift
//  ModernFoodWebsite
//
//  Created by apple on 16/5/30.
//  Copyright © 2016年 Lonnie. All rights reserved.
//

import UIKit

class NoticeListCell: UITableViewCell {
    var titleLabel = UILabel()
    var imageContentView = UIView()
    var imageHeightConstraint:NSLayoutConstraint?
    var avaitarImageView = UIImageView()
    var nicknameLabel = UILabel()
    var publishLabel = UILabel()
    var timeLabel = UILabel()
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style:style,reuseIdentifier:reuseIdentifier)
        initUI()
    }
    private func initUI() {
        contentView.addSubviews([titleLabel,imageContentView,avaitarImageView,nicknameLabel,publishLabel,timeLabel])
        titleLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.width.equalTo(contentView).offset(-31)
            make.top.equalTo(contentView).offset(12)
        }
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont.systemFontOfSize(16)
        imageContentView.snp_makeConstraints { (make) in
            make.centerX.equalTo(contentView)
            make.width.equalTo(titleLabel)
            make.top.equalTo(titleLabel.snp_bottom).offset(11.5)
            make.height.equalTo(0)
        }
        imageContentView.constraints.forEach { (constraint) in
            if constraint.firstAttribute == .Height {
                imageHeightConstraint = constraint
            }
        }
        
        avaitarImageView.snp_makeConstraints { (make) in
            make.leading.equalTo(imageContentView)
            make.top.equalTo(imageContentView.snp_bottom).offset(12)
            make.bottom.equalTo(contentView.snp_bottom).offset(-12)
            make.size.equalTo(CGSizeMake(20, 20))
        }
        avaitarImageView.layer.cornerRadius = 10
        avaitarImageView.layer.masksToBounds = true
        
        nicknameLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(avaitarImageView)
            make.leading.equalTo(avaitarImageView.snp_trailing).offset(4)
        }
        nicknameLabel.textColor = Style.themeColor1
        nicknameLabel.font = UIFont.systemFontOfSize(13)
        
        publishLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(avaitarImageView)
            make.leading.equalTo(avaitarImageView.snp_trailing).offset(8.5)
        }
        publishLabel.textColor = Style.textColor2
        publishLabel.text = "发布于"
        publishLabel.font = UIFont.systemFontOfSize(11)
        timeLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(avaitarImageView)
            make.leading.equalTo(publishLabel.snp_trailing).offset(4)
        }
        timeLabel.textColor = Style.textColor3
        timeLabel.font = UIFont.systemFontOfSize(11)
        setImages([])
    }
    
    func setImages(images:[UIImage]) {
        imageContentView.subviews.forEach { (view) in
            view.removeFromSuperview()
        }
        if images.count == 0 {
            imageHeightConstraint?.constant = 0
        } else {
            imageHeightConstraint?.constant = 75.5
            let gap = (ScreenWidth() - 31.0 - 75.5*4) / 3.0
            for i in 0..<images.count {
                let x = (75+gap)*CGFloat(i)
                let imageView = UIImageView(frame: CGRectMake(x, 0, 75, 75.5))
                imageContentView.addSubview(imageView)
                let image = images[i]
                imageView.image = image
            }
        }
    }
    
    func setImageUrls(urls:[String]){
    
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
